﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Database;
public partial class admin_Suatintuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var pm = new checkLogin();
        pm.redirectToLoginPage();  
        if (!IsPostBack)
            {
                if (Session["tenkh"] == "Admin")
                {
                    Response.Redirect("Login.aspx");
                }
                else
                {
                    ddlTenNhanVien.DataSource = KhachHang.DSTenNhanVien().Tables[0];
                    ddlTenNhanVien.DataTextField = "tenkh";
                    ddlTenNhanVien.DataValueField = "tenkh";
                    ddlTenNhanVien.DataBind();

                    Ngay();
                    Thang();
                    Nam();
                    XuLyXuatTT();
                }
            }
        }
        public bool KiemTraNgayThang(int nam, int thang, int ngay)
        {
            bool kq = true;

            try
            {
                DateTime dt = new DateTime(nam, thang, ngay);
            }
            catch
            {
                kq = false;
            }


            return kq;
        }
        public void XuLyXuatTT()
        {
             int matintuc = int.Parse(Request.QueryString["matintuc"].ToString());

            TinTuc tt = new TinTuc();
            tt = tt.ThongTinTT(matintuc);
            this.txtMaTinTuc.Text = tt.Matintuc.ToString();
            this.txtTieuDe.Text = tt.Tieude;
            this.fckchitiet.Text = tt.Chitiet;
            this.txtMoTa.Text = tt.Mota;       

        }

        void Ngay()
        {
            for (int i = 1; i <= 31; i++)
            {
                ddlngay.Items.Add(i.ToString());
            }
        }
        void Thang()
        {
            for (int i = 1; i <= 12; i++)
            {
                ddlthang.Items.Add(i.ToString());
            }
        }
        void Nam()
        {
            for (int i = 1950; i <= 2050; i++)
            {
                ddlnam.Items.Add(i.ToString());
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            int ngay = int.Parse(ddlngay.SelectedValue.ToString());
            int thang = int.Parse(ddlthang.SelectedValue.ToString());
            int nam = int.Parse(ddlnam.SelectedValue.ToString());
            int matintuc = int.Parse(txtMaTinTuc.Text);
            string hinhanh;
            if (!String.IsNullOrEmpty(UpLoadHinhAnh.FileName))
            {
                String path = "~/images/" + UpLoadHinhAnh.FileName;
                UpLoadHinhAnh.SaveAs(MapPath(path));
                hinhanh = UpLoadHinhAnh.FileName.ToString();
            }
            else
            {
                hinhanh = "no_image.jpg";
            }
            string tennhanvien = ddlTenNhanVien.SelectedValue.ToString();
            string chitiet = fckchitiet.ToString();
            if (KiemTraNgayThang(nam, thang, ngay))
            {
                DateTime ngaydang = new DateTime(nam, thang, ngay);
                TinTuc tt = new TinTuc();
                tt.CapNhatTT(matintuc, this.txtTieuDe.Text, this.txtMoTa.Text,chitiet, ngaydang, hinhanh,tennhanvien);
                Response.Redirect("HienThiTinTuc.aspx");
            }
            else
            {
                              
                //MessageBox.ShowMessage("Ngày tháng năm chưa hợp lệ",this.Page);
            }

        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            txtTieuDe.Text = "";
            txtMoTa.Text = "";            
        }
    
}
